package com.toocms.jinjuhang.dialog.specification

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.*
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.DialogFragment
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.BR
import com.toocms.jinjuhang.databinding.DialogSpecificationBinding

class SpecificationDialog : DialogFragment() {
    var binding: DialogSpecificationBinding? = null
    var viewModel: SpecificationModel? = null
    var goodsId: String? = null
    var goodsType: String? = null
    var goodsName: String? = null
        set(value) {
            field = value
            viewModel?.nameObservableField?.set(field)
        }
    var quantity: Int = 0
        set(value) {
            field = value
            viewModel?.quantityObservableInt?.set(field)
        }
    var selectedAttrIds: String? = null
    var selectedAttrNames: String? = null
    var onConfirmClickListener: ((tag: String?, attrIds: String, attrNames: String, quantity: String) -> Unit)? =
        null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(inflater, R.layout.dialog_specification, container, false)
        viewModel = SpecificationModel(this)
        viewModel!!.nameObservableField.set(goodsName)
        binding!!.setVariable(BR.specificationModel, viewModel)
        return binding!!.root
    }

    override fun onStart() {
        super.onStart()
        dialog?.run {
            setCancelable(true)
            setCanceledOnTouchOutside(true)
            window
        }?.apply {
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            setGravity(Gravity.BOTTOM)
            setLayout(
                WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT
            )
        }
    }
}